package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.MessageMapper;
import com.jt.pojo.Item;
import com.jt.pojo.Message;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author shaohe_zhang
 * @Date 2021/12/10 13:17
 * @Version 1.0.0
 */

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    //查询评论信息列表
    @Override
    public PageResult getMessageList(PageResult pageResult) {
        //1.构建分页对象  参数1: 第几页   参数2: 多少条
        Page<Message> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        //2.准备条件构造器 构建模糊查询
        QueryWrapper queryWrapper = new QueryWrapper();
        String query = pageResult.getQuery();
        boolean flag = StringUtils.hasLength(query);
        queryWrapper.like(flag,"from_name",query);

        //3.根据MP查询 实现分页数据的自动封装
        page = messageMapper.selectPage(page,queryWrapper);

        //4.获取数据,返回分页对象
        long total = page.getTotal();
        //获取分页结果
        List<Message> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }
    //新增留言信息
    @Override
    @Transactional
    public void saveMessage(Message message) {
        messageMapper.insert(message);
    }
    //通过id删除评论信息
    @Override
    public void deleteMessageById(Integer id) {
        messageMapper.deleteById(id);
    }

    //通过id修改评论信息
    @Override
    public void updateMessageById(Message message) {
        messageMapper.updateById(message);
    }

}
